﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using System.Data;
using System.Text.RegularExpressions;

namespace BLL
{
    public class PhongBLL
    {
        QLKSEntities db = new QLKSEntities();
        LoaiPhongBLL loaiPhongBLL = new LoaiPhongBLL();

        public List<Phong> getAllPhong()
        {
            //using (var db = new QLKSEntities())
            //{
            //    var query = from phong in db.Phongs select phong;
            //    return query.ToList();
            //}
            return db.Phongs.ToList();
        }

        public List<Phong> TimPhongbyMaPhong(string maLP)
        {
            return db.Phongs.Where(p => p.MaLP == maLP).ToList();
        }

        public string taoMaPhong(string maLP)
        {
            int soLonNhat, maPhong;

            //lay ma phong trong Loai Phong
            List<Phong> lstPhong = TimPhongbyMaPhong(maLP);

            //string pattern = "^";

            //foreach (var item in loaiPhongBLL.getAllLoaiPhong())
            //{
            //    pattern += "["+item.MaLP.Trim() + "]|";
            //    item.
            //}
            //pattern += "$";

            //Regex re = new Regex(@"P[A-C]");
            Regex reMa = new Regex(@"\d+");

            Phong pDau = lstPhong.FirstOrDefault();
            Match mlayMaPhongDau = reMa.Match(pDau.MaPhong);
            int maDau = int.Parse(mlayMaPhongDau.Value);

            if (lstPhong.FirstOrDefault() == null)
            {
                return "P" + maLP + "1";
            }
            else
            {
                soLonNhat = maDau;
            }

            //lay danh sach cac phong co ma chung la A B C
            foreach (Phong p in lstPhong)
            {
                Match mlayMaPhong = reMa.Match(p.MaPhong);
                int layMaPhong = int.Parse(mlayMaPhong.Value);

                soLonNhat = Math.Max(soLonNhat, layMaPhong);
            }
            maPhong = soLonNhat + 1;

            return "P" + maLP.Trim() + maPhong;
        }

        public bool addPhong(string maLP)
        {
            Phong p = new Phong()
            {
                MaPhong = taoMaPhong(maLP),
                MaLP = maLP,
                SoKHTrongPhong = 0,
                PhuThu = 0,
                TinhTrang = "",
                GhiChu = ""
            };

            try
            {
                db.Phongs.AddObject(p);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool xoaPhong(string maPhong)
        {
            Phong p = db.Phongs.Where(p1 => p1.MaPhong == maPhong).FirstOrDefault();
            try
            {
                db.Phongs.DeleteObject(p);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool suaPhong(string maPhong, string tinhTrang, string ghiChu)
        {
            Phong p = db.Phongs.Where(p1 => p1.MaPhong == maPhong).FirstOrDefault();
            try
            {
                p.TinhTrang = tinhTrang;
                p.GhiChu = ghiChu;
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
